package com.example.system.class03;

/**
 * @Date 2022/7/18
 * @Created by Jonathan
 */
public class Code07_EvenTimesOddTimes {

    public static void main(String[] args) {
        int[] arr = new int[]{1, 1, 8, 5, 5, 5, 8};
        System.out.println("odd is:" + getOddTimesNum1(arr));
        int[] arr1 = new int[]{6, 10, 6, 6, 8, 8, 12, 12};
        printOddTimesNum2(arr1);
    }

    /**
     * 只有一种数，出现奇数次
     */
    public static int getOddTimesNum1(int[] arr) {
        int ans = 0;
        for (int num : arr) {
            ans ^= num;
        }
        return ans;
    }

    /**
     * 有两种数，出现奇数次
     */
    public static void printOddTimesNum2(int[] arr) {
        int eor = 0;
        // 最后就是这两种数^的结果
        for (int num : arr) {
            eor ^= num;
        }
        // 取出最右侧的1
        int rightOne = eor & (-eor);
        int onlyOne = 0;
        for (int num : arr) {
            if ((num & rightOne) != 0) {
                onlyOne ^= num;
            }
        }
        System.out.println(onlyOne + "___" + (eor ^ onlyOne));
    }
}
